//
//  UIViewController+NavBar.m
//  SP2P
//
//  Created by mac on 16/1/13.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "UIViewController+NavBar.h"

@implementation UIViewController (NavBar)

- (void)initNavBarTitle:(NSString *)titleName
        andLeftItemName:(NSString *)leftItemName
       andRightItemName:(NSString *)rightItemName {
    
//    if(iOS7) {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
    self.view.backgroundColor = KbackgroundColor;
    [self.navigationController.navigationBar setBarTintColor:KColor];
    //
    self.navigationItem.title = titleName;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:18.0f],NSFontAttributeName, nil]];
    
    if (![leftItemName isEmpty]) {
        UIBarButtonItem *leftButtonItem;
        if ([leftItemName hasSuffix:@"*image"]) {
            leftItemName = [leftItemName substringToIndex:leftItemName.length - 6];
            leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:leftItemName] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        }else {
            if ([leftItemName isEqualToString:@"返回"]) {
                UIButton *lefuButton = [UIButton buttonWithFrame:CGRectMake(0, 0, 44, 44) backgroundColor:nil title:leftItemName titleColor:nil titleFont:15.0 target:self action:@selector(back) superview:nil];
                lefuButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
                [lefuButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
                [lefuButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
                [lefuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [lefuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                [lefuButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
                [lefuButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
                leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:lefuButton];
            }else {
                leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:leftItemName style:UIBarButtonItemStyleDone target:self action:@selector(back)];
            }
        }
        [leftButtonItem setTintColor:[UIColor whiteColor]];
        [leftButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:16.0f],NSFontAttributeName, nil] forState:UIControlStateNormal];
        self.navigationItem.leftBarButtonItem = leftButtonItem;
    }else {
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    if (![rightItemName isEmpty]) {
        UIBarButtonItem *rightButtonItem;
        if ([rightItemName hasSuffix:@"*image"]) {
            rightItemName = [rightItemName substringToIndex:rightItemName.length - 6];
            rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:rightItemName] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonClick)];
        }else {
            rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:rightItemName style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonClick)];
        }
        [rightButtonItem setTintColor:[UIColor whiteColor]];
        [rightButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:16.0f],NSFontAttributeName, nil] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = rightButtonItem;
    }else {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarButtonClick
{
    
}

- (void)initNavBarTitle:(NSString *)titleName andLeftImageName:(NSString *)leftImageName andRightImageName:(NSString *)rightImageName
{
    
    [self initLeftBarButtonItemWithImageName:leftImageName andColor:[UIColor whiteColor]];
    
    if (rightImageName.length > 0) {
        [self initRightBarButtonItemWithObj:[UIImage imageNamed:rightImageName] andColor:[UIColor whiteColor]];
    }
    
    self.navigationItem.title = titleName;
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)initNavBarTitle:(NSString *)titleName andLeftImageName:(NSString *)leftImageName andRightItem:(NSString *)rightItem
{
    [self initLeftBarButtonItemWithImageName:leftImageName andColor:[UIColor whiteColor]];
    
    if (rightItem.length > 0) {
        [self initRightBarButtonItemWithObj:rightItem andColor:[UIColor whiteColor]];
    }
    self.navigationItem.title = titleName;
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)initNavBarTitle:(NSString *)titleName andLeftImageName:(NSString *)leftImageName andRightButton:(id)rightButton andStatusBar:(UIStatusBarStyle)statusBar andBarTinColor:(UIColor *)barTinColor andTinColor:(UIColor *)tinColor
{
    [[UIApplication sharedApplication] setStatusBarStyle:statusBar];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationItem.title = titleName;
    
    [self.navigationController.navigationBar setBarTintColor:barTinColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0],NSForegroundColorAttributeName:tinColor}];
    
    [self initLeftBarButtonItemWithImageName:leftImageName andColor:tinColor];
    
    [self initRightBarButtonItemWithObj:rightButton andColor:tinColor];
    
}

- (void)initLeftBarButtonItemWithImageName:(NSString *)imgName andColor:(UIColor *)color
{
    if (imgName.length > 0) {
        UIBarButtonItem* leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imgName] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        [leftButtonItem setTintColor:color];
        self.navigationItem.leftBarButtonItem = leftButtonItem;
    }
}

- (void)initRightBarButtonItemWithObj:(id)obj andColor:(UIColor *)color
{
    if (obj != nil) {
        
        if ([obj isKindOfClass:[UIImage class]])
        {
            UIImage *img = (UIImage *)obj;
            UIBarButtonItem* rightButtonItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonClick)];
            [rightButtonItem setTintColor:color];
            self.navigationItem.rightBarButtonItem = rightButtonItem;
        }
        else if ([obj isKindOfClass:[NSString class]])
        {
            NSString *nameStr = (NSString *)obj;
            
            if (nameStr.length > 0) {
                UIBarButtonItem* rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:nameStr style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonClick)];
                [rightButtonItem setTintColor:color];
                self.navigationItem.rightBarButtonItem = rightButtonItem;
            }
            
        }
    }
}


- (void)setNavBarBackgroundColor:(UIColor *)color andStatusBar:(UIStatusBarStyle)statusBar
{
    [[UIApplication sharedApplication] setStatusBarStyle:statusBar];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:[CommonClass imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
   [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}


- (void)resetNavigationBar
{
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[CommonClass imageWithColor:KColor] forBarMetrics:UIBarMetricsDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

@end
